package com.student.dms;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.student.db.AccountDao;
import com.student.db.AccountEntity;
import com.student.db.AppDatabase;
import com.student.dms.databinding.ActivityMainBinding;

import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Single;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class MainActivity extends BaseActivity {

    private ActivityMainBinding activityMainBinding;
    private List<String>mTabs;
    private List  <Integer>mSelectedIcon;
    private List <Integer>mUnSelectedIcon;
    private List <Class>mFragments;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityMainBinding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(activityMainBinding.getRoot());
        //查询账号类型
        SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
        String username = sharedPreferences.getString("username", "");
        if(!TextUtils.isEmpty(username)){
            AppDatabase roomDatabase = DMSApplication.getApplication().getRoomDatabase();
            AccountDao accountDao = roomDatabase.provideAccountDao();
            Single<AccountEntity> studentFlow = accountDao.searchAccountByName(username);
            disposable.add(studentFlow.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(accountEntity -> {
                if(accountEntity.getType()==0){
                    //学生
                    mFragments=Arrays.asList(StudentHomeFragment.class,MineFragment.class);
                    mTabs=Arrays.asList("首页","我的");
                    mSelectedIcon= Arrays.asList(R.mipmap.ic_home_selected, R.mipmap.ic_mine_selected);
                    mUnSelectedIcon= Arrays.asList(R.mipmap.ic_home_normal, R.mipmap.ic_mine_normal);
                }else{
                    mFragments=Arrays.asList(AdminHomeFragment.class, AdminManagerFragment.class,MineFragment.class);
                    mTabs=Arrays.asList("首页","学生管理","我的");
                    mSelectedIcon= Arrays.asList(R.mipmap.ic_home_selected, R.mipmap.ic_vip_selected, R.mipmap.ic_mine_selected);
                    mUnSelectedIcon= Arrays.asList(R.mipmap.ic_home_normal,R.mipmap.ic_vip_normal, R.mipmap.ic_mine_normal);
                }

                FragmentStateAdapter fragmentStateAdapter = new FragmentStateAdapter(getSupportFragmentManager(), getLifecycle()) {
                    @Override
                    public int getItemCount() {
                        return mFragments.size();
                    }
                    @NonNull
                    @Override
                    public Fragment createFragment(int position) {
                        try {
                            return (Fragment) mFragments.get(position).getDeclaredConstructor().newInstance();
                        } catch (IllegalAccessException e) {
                            throw new RuntimeException(e);
                        } catch (InstantiationException e) {
                            throw new RuntimeException(e);
                        } catch (InvocationTargetException e) {
                            throw new RuntimeException(e);
                        } catch (NoSuchMethodException e) {
                            throw new RuntimeException(e);
                        }
                    }
                };
                activityMainBinding.viewpager.setAdapter(fragmentStateAdapter);
                activityMainBinding.viewpager.setUserInputEnabled(false);
                activityMainBinding.tableLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                    @Override
                    public void onTabSelected(TabLayout.Tab tab) {
                        tab.setIcon( mSelectedIcon.get(tab.getPosition()));
                    }
                    @Override
                    public void onTabUnselected(TabLayout.Tab tab) {
                        tab.setIcon( mUnSelectedIcon.get(tab.getPosition()));
                    }
                    @Override
                    public void onTabReselected(TabLayout.Tab tab) {

                    }
                });


                TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(activityMainBinding.tableLayout, activityMainBinding.viewpager, false, new TabLayoutMediator.TabConfigurationStrategy() {
                    @Override
                    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                        tab.setText(mTabs.get(position));
                        tab.setIcon(mUnSelectedIcon.get(position));
                    }
                });
                tabLayoutMediator.attach();
               // mViewBinding.tableLayout.getTabAt(2)?.select()
            }));
        }
    }
}
